package BTL;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import static javax.swing.JOptionPane.ERROR_MESSAGE;
import static javax.swing.JOptionPane.INFORMATION_MESSAGE;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author trtht_000
 */
public class DieuTri extends javax.swing.JFrame {

    /**
     * Creates new form help
     */
    public DieuTri() {
        initComponents();
    }

    public boolean huongDanDieuTri(String maBenh){
    {
        FileReader fileReader;
        String stream;
        String xauHuongDan = "";
        BufferedReader bufferReader;
        StringTokenizer tokenString;
        Event s;
        
        try
        {
            fileReader = new FileReader("src\\Dieu tri\\" + maBenh + ".txt");
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(null, "Không thể đọc file hướng dẫn điều trị", "Lỗi đọc file", ERROR_MESSAGE);
            return false;
        }
        bufferReader = new BufferedReader(fileReader);
        
        do
        {
            try
            {  
                stream = bufferReader.readLine();   
                if (stream == null)
                    break;
                xauHuongDan += stream;
                xauHuongDan += "\r\n";

                

            }
            catch(Exception e)
            {
                JOptionPane.showMessageDialog(null, "Lỗi đọc tệp sự kiện", "Lỗi khởi tạo", ERROR_MESSAGE);
                try
                {
                    bufferReader.close();
                }
                catch (Exception e1)
                {            
                }
                return false;
            }
        }
        while (true);
        textAreaDieuTri.setText(xauHuongDan);
        //textAreaDieuTri.setFocusable(true);
        try
        {
            bufferReader.close();
        }
        catch (Exception e)
        {            
        }
        return true;
    }
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        textAreaDieuTri = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();

        setTitle("Điều trị");

        jScrollPane1.setBorder(new javax.swing.border.MatteBorder(null));

        textAreaDieuTri.setEditable(false);
        textAreaDieuTri.setBackground(new java.awt.Color(102, 255, 204));
        textAreaDieuTri.setColumns(20);
        textAreaDieuTri.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        textAreaDieuTri.setLineWrap(true);
        textAreaDieuTri.setRows(5);
        textAreaDieuTri.setWrapStyleWord(true);
        textAreaDieuTri.setAutoscrolls(false);
        textAreaDieuTri.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jScrollPane1.setViewportView(textAreaDieuTri);

        jButton1.setText("Lưu kết quả");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 481, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(214, 214, 214)
                .addComponent(jButton1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 330, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27)
                .addComponent(jButton1)
                .addContainerGap(39, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        JFileChooser fileChooser;
        File file;
        String sFileName;
        
        fileChooser = new JFileChooser();
        fileChooser.showOpenDialog(this);
        file = fileChooser.getSelectedFile();
        if (file == null)
            return;
        
        sFileName = file.getAbsolutePath();
        System.out.println(sFileName);
        SaveResultToFile(sFileName);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void SaveResultToFile(String sFileName)
    {
        String stream;
        FileOutputStream fileOut;
        PrintWriter printFile;
        
        try
        {
            fileOut = new FileOutputStream(sFileName, false);
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(null, "Không ghi lại được kết quả", "Lỗi", ERROR_MESSAGE);
            return;
        }
        
        printFile = new PrintWriter(fileOut);
        
        stream = textAreaDieuTri.getText();
        
        printFile.write(stream);
        printFile.close();
        JOptionPane.showMessageDialog(this, "Lưu kết quả thành công", "Thông báo", INFORMATION_MESSAGE);
        
        return;
    }
    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        JFileChooser fileChooser;
        File file;
        String sFileName;
        
        fileChooser = new JFileChooser();
        fileChooser.showOpenDialog(this);
        file = fileChooser.getSelectedFile();
        if (file == null)
            return;
        
        sFileName = file.getAbsolutePath();
        System.out.println(sFileName);
        SaveResultToFile(sFileName);
    }       
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(DieuTri.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(DieuTri.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(DieuTri.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(DieuTri.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                DieuTri intro = new DieuTri();
                intro.setVisible(true);
                intro.setLocation(450, 200);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea textAreaDieuTri;
    // End of variables declaration//GEN-END:variables
}
